package cn.niit.letuseatdemo.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

//import cn.niit.hospital.common.enums.ExceptionCodeEnum;
//import cn.niit.hospital.exception.HospitalException;

/**
 * @Description:
 * @Author: yang_yong
 * @CreateTime： 2022/11/22 16:37
 */
@Slf4j
@Component
public class HttpUtils {

    /** HTTP 返回成功状态字符串 */
    public static final String HTTP_RESPONSE_RESULT_SUCCESS = "OK";

//    private static String SMS_SEND_URL = "https://dfsns.market.alicloudapi.com/data/send_sms";
    private static String SMS_SEND_URL;

//    private static String SMS_SEND_APPCODE = "82f48529f6474cd880a3ab7cce028c0b";    //你的AppCode
    private static String SMS_SEND_APPCODE;

    @Value("${sms.send.url}")
    public void setSmsSendUrl(String smsSendUrl){
        SMS_SEND_URL = smsSendUrl;
    }

    @Value("${sms.send.appcode}")
    public void setSmsSendAppcode(String smsSendAppcode){
        SMS_SEND_APPCODE = smsSendAppcode;
    }

    /**
     * @Description: 发送短信验证码
     * @Author: yang_yong
     * @CreateTime: 2022/11/22 20:43 
     * @param: phone 接收短信的手机号（给谁发送）
     * @param: verifyCode 要发送的短信验证码
     * @return: java.lang.String
     **/
    public static String sendSms(String phone, String verifyCode){
        log.info("【HttpUtils】短信验证码接口请求参数：url = {}, appcode = {}, phone = {}, verifycode = {}", SMS_SEND_URL, SMS_SEND_APPCODE, phone, verifyCode);
        /** 验证手机号、短信验证码是否为空？ */
//        if(!StringUtils.hasLength(phone.trim()) || !StringUtils.hasLength(verifyCode.trim())){
//            throw new HospitalException(ExceptionCodeEnum.PARAM_VALIDATION_NULL);
//        }
        HttpRequest post = HttpUtil.createPost(SMS_SEND_URL);
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73e3c139105
        headers.put("Authorization", "APPCODE " + SMS_SEND_APPCODE);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        post.addHeaders(headers);

        //准备 body 数据； TODO 如果使用其他公司（单位）短信接口，内容必须按照接口规范修改
        Map<String, Object> bodys = new HashMap<String, Object>();
        bodys.put("phone_number", phone);
        bodys.put("content", "code:" + verifyCode + ",expire_at:3"); //设置模板中的参数内容：短信验证码、3分钟
        bodys.put("template_id", "TPL_0001");   //短信模板编号

        post.form(bodys);   //设置请求体数据；
        post.timeout(2000);
        HttpResponse response = post.execute();
        String result =  response.body();
        JSONObject json = JSONObject.parseObject(result);
        String status = (String) json.get("status");
        if("OK".equals(status)){
            return HTTP_RESPONSE_RESULT_SUCCESS;
        } else {
            log.error("【HttpUtils】给【{}】发送短信验证码【{}】时发生异常：{}", phone, verifyCode, result);
            return result;
        }
    }
}